﻿using _8Group.MES.MeasuringUnit.DTO;
using Microsoft.AspNetCore.Mvc;
using Microsoft.VisualBasic;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace _8Group.MES.MeasuringUnit
{
    [ApiExplorerSettings(GroupName = "李明豫")]
    public class MeasuringUnitService:ApplicationService,IMeasuringUnitService
    {
        private readonly IRepository<MdUnitMeasure, int> _measuringUnitRepository;
        public MeasuringUnitService(IRepository<MdUnitMeasure, int> measuringUnitRepository)
        {
            _measuringUnitRepository = measuringUnitRepository;
        }
        /// <summary>
        /// 计量单位显示
        /// </summary>
        /// <param name="measureCode"></param>
        /// <param name="measureName"></param>
        /// <returns></returns>
        public async Task<string> GetMeasuringUnitAsync(string? measureCode, string? measureName)
        {
            var mdUnitMeasureList = await _measuringUnitRepository.GetListAsync();
            if (!string.IsNullOrEmpty(measureCode))
            {
                mdUnitMeasureList = mdUnitMeasureList.Where(x => x.MeasureCode.Contains(measureCode)).ToList();
            }
            if (!string.IsNullOrEmpty(measureName))
            {
                mdUnitMeasureList = mdUnitMeasureList.Where(x => x.MeasureName.Contains(measureName)).ToList();
            }
            return JsonConvert.SerializeObject(new
            {
                code = "0",
                data = mdUnitMeasureList,
                count = mdUnitMeasureList.Count,
                msg = "success"
            }, Formatting.Indented);
        }

        /// <summary>
        /// 新增计量单位
        /// </summary>
        /// <param name="input"></param>
        public async Task<CreateMeasuringUnit> AddMeasuringUnit(CreateMeasuringUnit input)
        {
            var addmdUnitMeasure = ObjectMapper.Map<CreateMeasuringUnit, MdUnitMeasure>(input);
            addmdUnitMeasure.PrimaryId = 1;
            addmdUnitMeasure.ChangeRate = 1;
            addmdUnitMeasure.CreateBy = "李明豫";
            addmdUnitMeasure.CreateTime = DateTime.Now;
            await _measuringUnitRepository.InsertAsync(addmdUnitMeasure);
            return ObjectMapper.Map<MdUnitMeasure, CreateMeasuringUnit>(addmdUnitMeasure);
        }

        /// <summary>
        /// 修改计量单位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<string> UpdateUnitMeasureById(UpdateMeasuringUnitDTO input)
        {
            var measureUnitList = await _measuringUnitRepository.GetAsync(m=>m.Id==input.Id);
            measureUnitList.UpdateBy = "李明豫";
            measureUnitList.UpdateTime = DateTime.Now;
            measureUnitList.MeasureCode = input.MeasureCode;
            measureUnitList.MeasureName = input.MeasureName;
            measureUnitList.PrimaryFlag = input.PrimaryFlag;
            measureUnitList.PrimaryId = input.PrimaryId;
            measureUnitList.ChangeRate = input.ChangeRate;
            measureUnitList.EnableFlag = input.EnableFlag;
            measureUnitList.Remark = input.Remark;
            measureUnitList.CreateBy = input.CreateBy;
            measureUnitList.CreateTime = input.CreateTime;
            await _measuringUnitRepository.UpdateAsync(measureUnitList);
            return JsonConvert.SerializeObject(new ResultResturn<UpdateMeasuringUnitDTO>
            {
                code = "200",
                data = null,
                count = 1,
                msg = "修改成功"
            });
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<int> DeleteMeasuringUnit(int id)
        {
            await _measuringUnitRepository.DeleteAsync(id);
            return 1;
        }
    }
}
